package com.nuoniu.sibanyun.controller.erp;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.erp.ErpProject;
import com.nuoniu.sibanyun.service.erp.IErpProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  项目前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-10-12
 */
@RestController
@RequestMapping("/erp/project")
@Api(value = "项目管理", tags = "项目管理")
public class ErpProjectController extends BaseController {

    @Autowired
    private IErpProjectService projectService;

    @ApiOperation("获取编码")
    @GetMapping("getCode")
    public HttpResult getCode() {
        return HttpResult.ok(projectService.getCode());
    }

    @GetMapping("pageList")
    @ApiOperation("获取列表")
    public HttpResult pageList(Page page, ErpProject project) {
        Page<ErpProject> projectPage = projectService.pageList(page, project);
        return HttpResult.ok(projectPage);
    }

    @PostMapping("save")
    @ApiOperation("保存")
    public HttpResult save(@RequestBody ErpProject project) {
        if(StringUtils.isBlank(project.getName())) {
            throw new NuoNiuException("项目名称不能为空");
        }
//        if(StringUtils.isBlank(project.getCode())) {
//            throw new NuoNiuException("项目编码不能为空");
//        }

        projectService.saveErpProject(project);
        return HttpResult.ok();
    }

    @ApiOperation("获取项目详情")
    @GetMapping("getDetail")
    public HttpResult getDetail(Long id) {
        return HttpResult.ok(projectService.getById(id));
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    public HttpResult delete(@RequestBody List<Long> ids) {
        projectService.deleteByIds(ids);
        return HttpResult.ok();
    }


}
